<?php

class Application_Model_UserMapper
{
    protected $_dbTable;
 
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
 
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_User');
        }
        return $this->_dbTable;
    }
 
    public function save(Application_Model_User $user)
    {
        $data = array(
            'name'      => $user->getName(),
            'firstname' => $user->getFirstname(),
            'email'     => $user->getEmail(),
            'login'     => $user->getLogin(),
            'password'  => $user->getPassword(),
            'updated'   => date('Y-m-d H:i:s'),
        );
 
        if (null === ($id = $user->getId())) {
            unset($data['id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }
 
    public function find($id)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result))
        {
            return null;
        }
        $row = $result->current();
        $user = new Application_Model_User();
        $user->setId($row->id)
             ->setName($row->name)
             ->setFirstname($row->firstname)
             ->setEmail($row->email)
             ->setLogin($row->login)
             ->setPassword($row->password)
             ->setUpdated($row->updated);
        return $user;
    }
 
    public function findOneByLogin($login)
    {
        $table = $this->getDbTable();
        $row = $table->fetchRow($table->select()->where('login = ?', $login));
        if( empty($row) )
         {
            return null;
        }
        $user = new Application_Model_User();
        $user->setId($row->id)
             ->setName($row->name)
             ->setFirstname($row->firstname)
             ->setEmail($row->email)
             ->setLogin($row->login)
             ->setPassword($row->password)
             ->setUpdated($row->updated);
        return $user;
    }
 
    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_User();
            $entry->setId($row->id)
                ->setName($row->name)
                ->setFirstname($row->firstname)
                ->setEmail($row->email)
                ->setLogin($row->login)
                ->setPassword($row->password)
                ->setUpdated($row->updated);
            $entries[] = $entry;
        }
        return $entries;
    }
    
    /**
     *Fonction qui supprime de la table user de la bbd la ligne correpsondant a l'id user passe en paramere.
     * @param integer $id
     * @return void
     */
    public function remove($id)
    {
        $table = $this->getDbTable();
        $table->delete('id = '.$id);
    }
}

